package com.api.base.controll;
import com.api.base.config.auth.JwtTokenUtil;
import com.api.base.model.SysRole;
import com.api.base.model.VerificationCode;
import com.api.base.service.VerificationCodeService;
import com.api.common.UtilFun;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.base.model.User;
import com.api.base.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.api.core.annotation.PowerEnable;
import io.swagger.annotations.*;



/**
* Created by wanghuiwen on 2021/03/17.
*/
@PowerEnable(name = "User",url = "/user")
@Api(value = "User", tags = {"User"})
@RestController
@RequestMapping("/user")
public class UserController extends Ctrl{
    @Resource
    private UserService userService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private VerificationCodeService verificationCodeService;

    @ApiOperation(value = "User添加", tags = {"User"}, notes = "User添加")
    @PostMapping(value="/add",name="User添加")
    public Result add(@ApiParam User user) {
        userService.save(user);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "User删除", tags = {"User"}, notes = "User删除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",required=true, value = "Userid", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/delete",name="User删除")
    public Result delete(@RequestParam Long id) {
        userService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "User修改", tags = {"User"}, notes = "User修改,对象主键必填")
    @PostMapping(value="/update",name="User修改")
    public Result update(@ApiParam User user) {
        userService.update(user);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "User详细信息", tags = {"User"}, notes = "User详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",required=true, value = "Userid", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/detail",name="User详细信息")
    public Result detail(@RequestParam Integer id) {
        User user = userService.findById(id);
        return ResultGenerator.genSuccessResult(user);
    }

    @ApiOperation(value = "User列表信息", tags = {"User"}, notes = "User列表信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "search", value = "查询条件json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/list", name = "User列表信息")
    public Result list(@RequestParam(defaultValue = "{}") String search,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        return userService.list(search, order, page, size);
    }

    @PostMapping(value = "/get/Role", name = "User列表信息")
    public Result getRole(Long userId) {
        return ResultGenerator.genSuccessResult(userService.getMenu(userId));
    }


    @ApiOperation(value = "User修改登录密码", tags = {"User"}, notes = "User修改,对象主键必填")
    @PostMapping(value="/update/password",name="User修改登录密码")
    public Result updatePassword(@RequestParam String password ,@RequestParam String oldpassword , Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();

        Result result = userService.updatePassword(userId,oldpassword,password);


        return result;
    }

    @ApiOperation(value = "User修改交易密码", tags = {"User"}, notes = "User修改,对象主键必填")
    @PostMapping(value="/transaction/password/update",name="User修改交易密码")
    public Result transactionPasswordUpdate(@RequestParam String password ,@RequestParam String oldpassword , Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();

        Result result = userService.transactionPasswordUpdate(userId,oldpassword,password);


        return result;
    }

    @ApiOperation(value = "发送验证码", tags = {"User"}, notes = "发送验证码")
    @PostMapping(value="/send/verification",name="发送验证码")
    public Result sendVerification(@RequestParam String email,@RequestParam(defaultValue = "1") Integer type) {
        VerificationCode verificationCode = new VerificationCode();
        verificationCode.setVerificationCode(UtilFun.getNumRandom(4));
        verificationCode.setEmail(email);
        verificationCode.setType(type);
        //15分钟
        verificationCode.setExpirationTime(new Date().getTime()+900000);
        return verificationCodeService.send(verificationCode);
    }

    @PostMapping(value="/disable",name="禁用用户")
    public Result disable(Boolean disable,Long id) {
        User user = userService.findById(id);
        user.setDisable(disable);
        userService.update(user);
        return ResultGenerator.genSuccessResult();
    }
}
